/* Copyright 2010  Jacob Brunson  (email : gdocsuploader.gpl.license@jacobbrunson.com )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

function UserMetadata(auth_obj)
{
    air.trace("New metadata object");
    var userMetadata = this;
    this.auth = auth_obj;
    this.initialized = false;
    this.loading = false;
    this.quotaTotal;
    this.quotaUsed;
    this.documentTypes = new Array();
    this.documentMaxSize;
    this.spreadsheetTypes = new Array();
    this.spreadsheetMaxSize;
    this.presentationTypes = new Array();
    this.presentationMaxSize;
    this.drawingTypes = new Array();
    this.drawingMaxSize;
    this.pdfMaxSize;
    this.uploadAny = false;
    this.fileMaxSize;

    this.callback;

    this.getURL = function()
    {
        return "https://docs.google.com/feeds/metadata/"+this.auth.email;
    }

    this.getFeed = function(callback)
    {
        air.trace("getting metadata feed...");
        if (callback) {
            this.callback = callback;
        } else if (!this.callback) {
            air.trace("setting an empty callback function");
            this.callback = function(){};
        }
        if (this.loading) {
            air.trace("The user metadata is already being retrieved");
            return;
        }
        if (!this.auth || this.auth.email == "") {
            air.trace("No email to use for getting metadata");
            return;
        }

        this.loading = true;
        hadErrors = false;
        air.trace("feed getting setup complete");
        url = this.getURL();
        air.trace("Getting user metadata feed ", url, this.auth.logintoken, "x");
        new Ajax.Request(url, {
            requestHeaders : {'Authorization': "GoogleLogin auth="+this.auth.logintoken,
                              'GData-Version': "3.0"},
            onSuccess: function(response) {
                air.trace("Got user metadata feed, parsing");
                inputs = response.responseXML.getElementsByTagName('importFormat');
                air.trace("inputs", inputs, inputs.length);
                for (i = 0; i < inputs.length; i++) {
                    target = inputs[i].attributes.getNamedItem('target').value;
                    the_value = inputs[i].attributes.getNamedItem('source').value;
                    air.trace("target", target, "value", the_value);
                    if (target == 'document') {
                        userMetadata.documentTypes.push(the_value);
                    } else if (target == 'spreadsheet') {
                        userMetadata.spreadsheetTypes.push(the_value);
                    } else if (target == 'presentation') {
                        userMetadata.presentationTypes.push(the_value);
                    } else if (target == 'drawing') {
                        userMetadata.drawingTypes.push(the_value);
                    }
                }
                sizes = response.responseXML.getElementsByTagName('maxUploadSize');
                air.trace("sizes", sizes.length);
                for (i = 0; i < sizes.length; i++) {
                    kind = sizes[i].attributes.getNamedItem('kind').value;
                    the_value = parseInt(sizes[i].firstChild.textContent);
                    air.trace("type", kind, sizes[i], "value", sizes[i].firstChild.textContent, the_value);
                    if (kind == 'document') {
                        userMetadata.documentMaxSize = the_value;
                    } else if (kind == 'spreadsheet') {
                        userMetadata.spreadsheetMaxSize = the_value;
                    } else if (kind == 'presentation') {
                        userMetadata.presentationMaxSize = the_value;
                    } else if (kind == 'pdf') {
                        userMetadata.pdfMaxSize = the_value;
                    } else if (kind == 'file') {
                        userMetadata.fileMaxSize = the_value;
                    } else if (kind == 'drawing') {
                        userMetadata.drawingMaxSize = the_value;
                    }
                }
                features = response.responseXML.getElementsByTagName('featureName');
                for (i = 0; i < features.length; i++) {
                    if (features[i].firstChild.textContent == 'upload_any') {
                        userMetadata.uploadAny = true;
                    }
                }
                if (response.responseXML.getElementsByTagName('quotaBytesTotal').length == 1) {
                    userMetadata.quotaTotal = parseInt(response.responseXML.getElementsByTagName('quotaBytesTotal')[0].firstChild.textContent);
                } else {
                    air.trace("Too many quotaBytesTotal elements");
                    hadErrors = true;
                }
                if (response.responseXML.getElementsByTagName('quotaBytesTotal').length == 1) {
                    userMetadata.quotaTotal = parseInt(response.responseXML.getElementsByTagName('quotaBytesUsed')[0].firstChild.textContent);
                } else {
                    air.trace("Too many quotaBytesTotal elements");
                    hadErrors = true;
                }
                if (!hadErrors) {
                    userMetadata.initialized = true;
                    userMetadata.loading = false;
                } else {
                    air.trace("There were errors getting all the metadata");
                }
                if (userMetadata.callback) {
                    userMetadata.callback();
                } else {
                    air.trace("Couldn't find callback function");
                }
            },
            onFailure: function(response) {
                air.trace("Failure getting user metadata feed", response.responseText);
                userMetadata.loading = false;
                if (userMetadata.callback) {
                    userMetadata.callback();
                } else {
                    air.trace("Couldn't find callback function");
                }
            }
        });
    }

    this.allowedSize = function(mime_type, bytes)
    {
        air.trace("Checking size of", mime_type, bytes);

        if (!this.initialized) {
            air.trace("Sinced this isn't initialized, returning true");
            return true;
        }

        if (mime_type == 'application/pdf' && bytes <= this.pdfMaxSize) return true;

        for (i = 0; i < this.documentTypes.length; i++) {
            if (mime_type == this.documentTypes[i] && bytes <= this.documentMaxSize) {
                air.trace(mime_type, bytes, "<", this.documentMaxSize);
                return true;
            }
        }
        
        for (i = 0; i < this.spreadsheetTypes.length; i++) {
            if (mime_type == this.spreadsheetTypes[i] && bytes <= this.spreadsheetMaxSize) return true;
        }

        for (i = 0; i < this.presentationTypes.length; i++) {
            if (mime_type == this.presentationTypes[i] && bytes <= this.presentationMaxSize) {
                air.trace(mime_type, bytes, "<", this.presentationMaxSize);
                return true;
            }
        }

        for (i = 0; i < this.drawingTypes.length; i++) {
            if (mime_type == this.drawingTypes[i] && bytes <= this.drawingMaxSize) {
                air.trace(mime_type, bytes, "<", this.drawingMaxSize);
                return true;
            }
        }

        if (this.uploadAny && bytes <= this.fileMaxSize) return true;

            air.trace("size too big or file type not allowed");

        return false;
    }

}